﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using edowork.com.SQL2005;
using edowork.com.SQL;
using edowork.com.Model;

namespace InvoicingsysControl
{
    public partial class levle1 : Form
    {

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void Bind()
        {
            DataSet dslist = new DataSet();
            DataSet dsResult = new DataSet();

            dslist.Tables.Add("list");

            dslist.Tables["list"].Columns.Add("menuid");
            dslist.Tables["list"].Columns.Add("menuremark");
            dslist.Tables["list"].Columns.Add("levels");
            dslist.Tables["list"].Columns.Add("parentid");
            dslist.Tables["list"].Columns.Add("sortindex");
            dslist.Tables["list"].Columns.Add("url");


            edowork.com.SQL2005.Execute exec = new edowork.com.SQL2005.Execute(System.Configuration.ConfigurationManager.AppSettings["SqlServer"].ToString());


            dsResult = exec.ExecSQLQueryDataSet("select * from menus where levels=1 order by sortindex");


            if((dsResult != null) && (dsResult.Tables.Count > 0))
            {
                foreach (DataRow dr in dsResult.Tables[0].Rows)
                {
                    DataRow drnew = dslist.Tables["list"].NewRow();

                    drnew["menuid"] = dr["menuid"].ToString();
                    drnew["menuremark"] = dr["menuremark"].ToString();
                    drnew["url"] = dr["url"].ToString();
                    drnew["levels"] = dr["levels"].ToString();
                    drnew["parentid"] = dr["parentid"].ToString();
                    drnew["sortindex"] = dr["sortindex"].ToString();

                    dslist.Tables["list"].Rows.Add(drnew);
                }
            }  

            dgv_menulist.AutoGenerateColumns = true;
            dgv_menulist.DataSource = dslist.Tables["list"];
        }

        public levle1()
        {
            InitializeComponent();
        }

        private void levle1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;

            Bind();
        }

        //添加
        private void btn_Add_Click(object sender, EventArgs e)
        {
            string menuid = txt_menuid.Text.Trim();
            string menuremark = txt_menuremark.Text.Trim();
            string url = txt_url.Text.Trim();
            string sortindex = num_sortindex.Value.ToString();

            if (String.IsNullOrEmpty(menuid))
            {
                error_msg.SetError(txt_menuid, "请输入菜单编码。");
                txt_menuid.Focus();
                return;
            }

            if (String.IsNullOrEmpty(menuremark))
            {
                error_msg.SetError(txt_menuremark, "请输入菜单描述。");
                txt_menuremark.Focus();
                return;
            }

            //保存入数据库
            edowork.com.SQL2005.Execute exec = new edowork.com.SQL2005.Execute(System.Configuration.ConfigurationManager.AppSettings["SqlServer"].ToString());

            int result = 0;

            string sql = (new edowork.com.Model.menusInfo()).SQL_Insert();

            sql = String.Format(sql, menuid, menuremark, url, 1, "0", sortindex);

            result = exec.ExecSQLQueryInt(sql);

            if (result <= 0)
            {
                MessageBox.Show("保存菜单失败，请检查是否存在相同的菜单编码。");
                return;
            }

            MessageBox.Show("保存成功。");
            Bind();

            txt_menuid.Text = "";
            txt_menuremark.Text = "";
            txt_url.Text = "";
        }

        //修改
        private void btn_Modify_Click(object sender, EventArgs e)
        {
            string menuid = txt_menuid.Text.Trim();
            string menuremark = txt_menuremark.Text.Trim();
            string url = txt_url.Text.Trim();
            string sortindex = num_sortindex.Value.ToString();

            if (String.IsNullOrEmpty(menuid))
            {
                error_msg.SetError(txt_menuid, "请输入菜单编码。");
                txt_menuid.Focus();
                return;
            }

            if (String.IsNullOrEmpty(menuremark))
            {
                error_msg.SetError(txt_menuremark, "请输入菜单描述。");
                txt_menuid.Focus();
                return;
            }

            //保存入数据库
            edowork.com.SQL2005.Execute exec = new edowork.com.SQL2005.Execute(System.Configuration.ConfigurationManager.AppSettings["SqlServer"].ToString());

            int result = 0;

            string sql = "update menus set menuremark='{1}', url='{2}', sortindex={3} where menuid='{0}'";

            sql = String.Format(sql, menuid, menuremark, url, sortindex);

            result = exec.ExecSQLQueryInt(sql);

            if (result <= 0)
            {
                MessageBox.Show("修改菜单失败，请检查是否数据已变动。");
                return;
            }

            MessageBox.Show("修改成功。");
            Bind();
        }

        //刷新
        private void btn_refresh_Click(object sender, EventArgs e)
        {
            Bind();
        }

        //删除
        private void btn_delete_Click(object sender, EventArgs e)
        {
            if (dgv_menulist.Rows.Count <= 0)
            {
                return;
            }

            if (DialogResult.No == MessageBox.Show("删除菜单吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                return;
            }

            string menuid = dgv_menulist.Rows[dgv_menulist.CurrentRow.Index].Cells["menuid"].Value.ToString();

            //删除数据
            edowork.com.SQL2005.Execute exec = new edowork.com.SQL2005.Execute(System.Configuration.ConfigurationManager.AppSettings["SqlServer"].ToString());

            int result = 0;

            string sql = String.Format("delete from menus where menuid='{0}'", menuid);

            result = exec.ExecSQLQueryInt(sql);

            if (result < 0)
            {
                MessageBox.Show("删除菜单失败，请检查是否数据已变动。");
                return;
            }

            MessageBox.Show("删除成功。");
            Bind();
        }

        //点击
        private void dgv_menulist_Click(object sender, EventArgs e)
        {
            if (dgv_menulist.Rows.Count <= 0)
            {
                return;
            }

            string menuid = dgv_menulist.Rows[dgv_menulist.CurrentRow.Index].Cells["menuid"].Value.ToString();

            //读取数据
            edowork.com.SQL2005.Execute exec = new edowork.com.SQL2005.Execute(System.Configuration.ConfigurationManager.AppSettings["SqlServer"].ToString());

            DataSet ds = new DataSet();

            string sql = String.Format("select * from menus where menuid='{0}'", menuid);

            ds = exec.ExecSQLQueryDataSet(sql);

            if ((ds == null) || (ds.Tables.Count <= 0))
            {
                return;
            }

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                txt_menuid.Text = dr["menuid"].ToString();
                txt_menuremark.Text = dr["menuremark"].ToString();
                txt_url.Text = dr["url"].ToString();
                num_sortindex.Value = Int32.Parse(dr["sortindex"].ToString());
            }
        }
    }
}
